<?php

class PageStatics {

	const __PAGE_HIT_TABLE__ = 'pft_page_hit';	//操作表名

	protected $db;	//db link

	public function __construct($db) {
        echo 'abcd';
		$this->db = $db;
	}

	/**
     * 获取各个页面的点击统计
     * @access  public
     * @param string $order 排序
     * @param string $limit 分页预留
     * @return array
     */
	public function get_page_hits($order = 'hits desc', $limit = '0,10') {
		$sql = sprintf("select * from %s order by %s %s",
					self::__PAGE_HIT_TABLE__, 
					$order,
					$limit ? ' limit ' . $limit : $limit 
				);
		$result = $this->db->query($sql);
		$data = array();
		while ($row = $result->fetch_assoc()) {
			$data[] = $row;
		}
		return $row;
	}
	
	/**
     * 增加页面点击量
     * @access  public
     * @param string $name 页面名称
     * @param string $identify 页面标识
     * @return void
     */
	public function add_hit($name, $identify) {
        echo 'abc';
		if (!$this->_page_has_exists($identify)) {
			$this->_init_page_record($name, $identify);
		} else {
			$sql = sprintf("update %s set hits = hits + 1, last_hit_time = %d where page_identify = '%s'",
						self::__PAGE_HIT_TABLE__, 
						time(),
						$identify
					);
            echo $sql;
			$this->db->query($sql);

		}

	}

	/**
     * 页面记录是否已经存在
     * @access private
     * @param string $name 页面名称
     * @return bool
     */
	private function _page_has_exists($identify) {
		$sql = sprintf("select id from %s where page_identify = '%s'", self::__PAGE_HIT_TABLE__, $identify);
		$result = $this->db->query($sql);
		return $result->num_rows ? true : false;
	}

	/**
     * 添加新的页面记录
     * @access private
     * @param string $name 页面名称
     * @param string $identify 页面标识
     * @return void
     */
	private function _init_page_record($name, $identify) {
		$sql = sprintf("insert into %s (page_name, page_identify, hits, last_hit_time) values ('%s', '%s', %d, %d)",
					self::__PAGE_HIT_TABLE__,
					$name,
					$identify,
					1,
					time()
				);
		$this->db->query($sql);
	}
}

//if (!$_SESSION['memberID']) {
//	exit();
//}

include('/var/www/html/new/d/common/autoload.php');

$name = 'aaa';//safetxt($_POST['name']);
$identify = 'bbb';//safetxt($_POST['identify']);
if ($name && $identify) {
	$Page = new PageStatics($GLOBALS['le']);
	$Page->add_hit($name, $identify);
}




